#include <iostream>
#include <vector>
using namespace std;

class Solution {
public:
    long long minTime(vector<int>& skill, vector<int>& mana) {
        vector <long long> fintime;
        int n = skill.size();
        int m = mana.size();
        long long ctime = 0;
        for (int i = 0; i < n; i++)
        {
            fintime.push_back(ctime + skill[i] * mana[0]);
            ctime = fintime[i];
        }
        for (int j = 1; j < m; j++)
        {
            long long del = 0;
            ctime = 0;
            for (int i = 0; i < n; i++)
            {
                ctime = max(ctime, fintime[i] - del);
                del += skill[i] * mana[j];
            }
            for (int i = 0; i < n; i++)
            {
                fintime[i] = ctime + skill[i] * mana[j];
                ctime = fintime[i];
            }
        }
        return fintime[n - 1];
    }
};

int main()
{
    vector<int> skill = { 1, 5, 2, 4 }, mana = { 5, 1, 4, 2 };
    cout << Solution().minTime(skill, mana);
    return 0;
}